﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.LTR.Clients
{
    public partial class ClienteditWoForm : Bll.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];
            }
            errMsg.Visible = false;
            LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (Request.QueryString["Edit"] == null)
                return;
            int woId = int.Parse(Request.QueryString["Edit"]);
            Bll.clsWorkOrder wo = new MLY.Bll.clsWorkOrder(woId, true);
            e.Result = wo.Data;
        }

        protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            _woId = int.Parse(e.NewValues["WorkOrderId"] as string);
            decimal quantitySent;
            DateTime actualTst, plannedTst;
            DateTime? actual = null, planned = null;
            for (int i = 0; i < e.NewValues.Values.Count; i++)
                if (e.NewValues[i] != null && e.NewValues[i].ToString() == "")
                    e.NewValues[i] = null;
            if (decimal.TryParse((FormView1.FindControl("quantSentTxt") as TextBox).Text, out quantitySent))
            {
                if (DateTime.TryParse(e.NewValues["ActualDelivaryDate"] as string, out actualTst))
                    actual = actualTst;
                if (DateTime.TryParse(e.NewValues["PlannedDelivaryDate"] as string, out plannedTst))
                    planned = plannedTst;

                decimal woQuantity = e.NewValues["Quantity"] == null ? 0 : decimal.Parse((string)e.NewValues["Quantity"]);
                if (quantitySent < woQuantity)
                {
                    Bll.clsWorkOrder.CreateDist(_woId, quantitySent, actual, planned);

                    // Reset WO fields since they are related to the new dist. not to the WO.
                    e.NewValues["PlannedDelivaryDate"] = e.OldValues["PlannedDelivaryDate"];
                    e.NewValues["ActualDelivaryDate"] = e.OldValues["ActualDelivaryDate"];
                    // If it less that the Wo Q. then decrease the WO Q. to the remaining Q. (also, dist. line was added earlier in this code).
                    e.NewValues["Quantity"] = woQuantity - quantitySent;
                }
                else if (quantitySent > woQuantity)
                {
                    e.NewValues["Quantity"] = quantitySent;
                }
            }
            e.SetValueToNull("PlannedDelivaryDate");
            e.SetValueToNull("ActualDelivaryDate");
        }

        protected void goBackBtn_Click(object sender, EventArgs e)
        {
            Response.RedirectTo(ViewState["Referer"].ToString());
        }

        protected void LinqDataSource1_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            successMsg.Visible = true;
            Bll.clsWorkOrder.UpdateDists(_woId);
        }


        #region Fields
        // ================================
        int _woId = 0;
        // ================================
        #endregion
    }
}
